package fr.esiag.isidis.epp.Interface;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import fr.esiag.isidis.epp.generatedClasses.CdcExportType;
import fr.esiag.isidis.epp.generatedClasses.CourbeDeCharge;
import fr.esiag.isidis.epp.generatedClasses.PointCdc;
import fr.esiag.isidis.epp.generatedClasses.PointConso;
import fr.esiag.isidis.epp.generatedClasses.Query;
import fr.esiag.isidis.epp.generatedClasses.QueryExportCdc;
import fr.esiag.isidis.epp.search.PointCdcService;
import fr.esiag.isidis.epp.service.JaxbConverter;
import fr.esiag.isidis.epp.util.Utils;

public class Visulisation {
	public String makeVisual(String Id, List<String> lcdc, Data d) {
		String datedeb = d.getDatedeb();
		String datefin = d.getDatefin();
		String res = Id+";" + datedeb +";" + datefin +";";
		for(String c : lcdc) {
			res += c+";";
		}
		return res;
	}
	public String sendData(PointConso pc , Data d) {
		String idString = pc.getIDSite();
		CourbeDeCharge cdc = pc.getCourbeDeCharge();
		List<String> lcdc = new ArrayList<String>();
		for(PointCdc p : cdc.getPointCdc() ) {
			String puissance = String.valueOf(p.getPuissanceSoutire());
			lcdc.add(puissance);
		}
		return makeVisual(idString, lcdc,d);
	}

	public String traiteVisual(String json) {
		TakeData t = new TakeData();
		Data d = t.takeDataFromString(json);
		QueryExportCdc qe = new QueryExportCdc();
		for (Integer s : d.getId()) {
		Query q = new Query();
		q.setDateDebut(Utils.convertDateToIntSite(d.getDatedeb()));
		q.setDateFin(Utils.convertDateToIntSite(d.getDatefin()));
		q.setHeureDebut(0);
		q.setHeureFin(1200);
		q.setIDSite(Utils.transformeIdnn(Utils.getIdnn(s)));
		q.setType(CdcExportType.INDIVIDUEL);
		qe.getQuery().add(q);
		}
		ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/epp-context.xml");
		PointCdcService pt = context.getBean(PointCdcService.class);
		PointConso pc =  pt.traiteQuery(qe);
		
		Visulisation v = new Visulisation();
		return(v.sendData(pc,d));
	}
	public static void main(String[] args) {
		/*ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/epp-context.xml");
		JaxbConverter converter = context.getBean(JaxbConverter.class);
		// unmashal xml file to java object 
		QueryExportCdc pp = converter.unmarshalerPointConso("src/main/resources/query.xml");
		List<Query> list=pp.getQuery();
		PointCdcService pt = context.getBean(PointCdcService.class);
		PointConso pc =  pt.traiteQuery(pp);*/
		/*String json = "{\"datedeb\":\"01/01/2011\",\"datefin\":\"10/12/2012\",\"id\":[1],\"filename\":\"test\"}";
		TakeData t = new TakeData();
		Data d = t.takeDataFromString(json);
		QueryExportCdc qe = new QueryExportCdc();
		for (Integer s : d.getId()) {
		Query q = new Query();
		q.setDateDebut(Utils.convertDateToIntSite(d.getDatedeb()));
		q.setDateFin(Utils.convertDateToIntSite(d.getDatefin()));
		q.setHeureDebut(0);
		q.setHeureFin(1200);
		q.setIDSite(Utils.transformeIdnn(Utils.getIdnn(s)));
		q.setType(CdcExportType.INDIVIDUEL);
		qe.getQuery().add(q);
		}
		ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/epp-context.xml");
		PointCdcService pt = context.getBean(PointCdcService.class);
		PointConso pc =  pt.traiteQuery(qe);
		
		Visulisation v = new Visulisation();
		System.out.println(v.sendData(pc,d));
	}*/
		String json = "{\"datedeb\":\"01/01/2011\",\"datefin\":\"10/12/2012\",\"id\":[1],\"filename\":\"test\"}";
		Visulisation v = new Visulisation();
		String a  = v.traiteVisual(json);
		System.out.println(a);
	}
}
